package com.googlecode.maratische.google.gui;

import static com.googlecode.maratische.google.MainController.getMainController;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import com.googlecode.maratische.google.BaseException;
import com.googlecode.maratische.google.model.Item;

/**
 * Адаптер мыши для всплывающего меню
 * @author maratische
 *
 */
public class PopupListenerDisplayPane extends MouseAdapter {
	private JPopupMenu feedsPopup = null;
	private JMenuItem menuItemPrev = null;
	private JMenuItem menuItemNext = null;
	private JMenuItem menuItemOpenBrowser = null;
	private JMenuItem menuItemcopyUrlToBrowser = null;
	private JMenuItem menuItemLabel = null;
	private JMenuItem menuItemExpand = null;
	private JMenuItem menuItemFullScreen = null;
	private JMenuItem menuItemSpamLabeled = null;
	private JMenuItem menuItemReadLabeled = null;
	private JMenuItem menuItemSourceCode = null;
	private JMenuItem menuItemViewItemNew = null;
	private JMenuItem menuItemViewItemAll = null;
	private JMenuItem menuItemFontSizeUp = null;
	private JMenuItem menuItemFontSizeDown = null;
	

	public PopupListenerDisplayPane() {
		this.feedsPopup =  createRightFeedsPopup();
	}
	
	private JPopupMenu createRightFeedsPopup() {
		JPopupMenu feedsPopup = new JPopupMenu();
		menuItemPrev = new JMenuItem(getBundleMessage("itemlist.previous") + " - left");
		feedsPopup.add(menuItemPrev);
		menuItemPrev.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getMainController().itemsListSelectLeft();
			}
		});
		menuItemNext = new JMenuItem(getBundleMessage("itemlist.next") + " - right");
		feedsPopup.add(menuItemNext);
		menuItemNext.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getMainController().itemsListSelectRight();
			}
		});
		menuItemOpenBrowser = new JMenuItem(getBundleMessage("itemlist.open.browser") + " - ctrl-Enter");
		feedsPopup.add(menuItemOpenBrowser);
		menuItemOpenBrowser.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					getMainController().openInBrowserSelectItem();
				} catch (Exception e1) {
					getMainController().showError("Ошибка открытия в браузере", e1);
				}
			}
		});
		menuItemcopyUrlToBrowser = new JMenuItem(getBundleMessage("itemlist.copyUrlToBrowser"));
		feedsPopup.add(menuItemcopyUrlToBrowser);
		menuItemcopyUrlToBrowser.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					getMainController().copySelectedItemUrlToClipboard();
			}
		});
		menuItemLabel = new JMenuItem(getBundleMessage("itemlist.label"));
		feedsPopup.add(menuItemLabel);
		menuItemLabel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					getMainController().openLabelFormForSelectedItem();
				} catch (SQLException e1) {
					getMainController().showError("Ошибка при создании метки", e1);
				} catch (BaseException e1) {
					getMainController().showError("Ошибка при создании метки", e1);
				}
			}
		});
		menuItemExpand = new JMenuItem(getBundleMessage("itemlist.expand.page") + " - u");
		feedsPopup.add(menuItemExpand);
		menuItemExpand.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getMainController().doExpandWindow();
			}
		});
		menuItemFullScreen = new JMenuItem(getBundleMessage("itemlist.full.screen") + " - F11");
		feedsPopup.add(menuItemFullScreen);
		menuItemFullScreen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getMainController().doFullScreenMode();
			}
		});
		menuItemReadLabeled = new JMenuItem(getBundleMessage("itemlist.read.labeled"));
		feedsPopup.add(menuItemReadLabeled);
		menuItemReadLabeled.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					getMainController().markSelectedItemsAsRead();
				} catch (Exception e1) {
					getMainController().showError(e1);
				}
			}
		});
		menuItemSpamLabeled = new JMenuItem(getBundleMessage("itemlist.spam.labeled"));
		feedsPopup.add(menuItemSpamLabeled);
		menuItemSpamLabeled.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					getMainController().showSpanFormForSelectedItem();
				} catch (Exception e1) {
					getMainController().showError(e1);
				}
			}
		});
		menuItemViewItemNew = new JMenuItem(getBundleMessage("itemlist.show.new"));
		feedsPopup.add(menuItemViewItemNew);
		menuItemViewItemNew.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					getMainController().setItemMenuViewAllStatus(false);
					getMainController().selectFeedUpdate();
				} catch (Exception e1) {
					getMainController().showError(e1);
				}
			}
		});
		menuItemViewItemAll = new JMenuItem(getBundleMessage("itemlist.show.all"));
		feedsPopup.add(menuItemViewItemAll);
		menuItemViewItemAll.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					getMainController().setItemMenuViewAllStatus(true);
					getMainController().selectFeedUpdate();
				} catch (Exception e1) {
					getMainController().showError(e1);
				}
			}
		});
		menuItemSourceCode = new JMenuItem(getBundleMessage("itemlist.source.code") + " - ctrl-u");
		feedsPopup.add(menuItemSourceCode);
		menuItemSourceCode.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getMainController().openSourceCodeSelectItem();
			}
		});
		menuItemFontSizeUp = new JMenuItem(getBundleMessage("itemlist.font.up") + " - ctrl-up");
		feedsPopup.add(menuItemFontSizeUp);
		menuItemFontSizeUp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getMainController().fontSizeUp();
			}
		});
		menuItemFontSizeDown = new JMenuItem(getBundleMessage("itemlist.font.down") + " - ctrl-down");
		feedsPopup.add(menuItemFontSizeDown);
		menuItemFontSizeDown.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getMainController().fontSizeDown();
			}
		});
		return feedsPopup;
	}
	
	
    public void mousePressed(MouseEvent e) {
        maybeShowPopup(e);
    }

    public void mouseReleased(MouseEvent e) {
        maybeShowPopup(e);
    }

    private void maybeShowPopup(MouseEvent e) {
        if (e.isPopupTrigger()) {
        	Item selectedItem = getMainController().getSelectedItem();
        	boolean isSelected = (selectedItem != null);
        	menuItemNext.setEnabled(isSelected && getMainController().itemsListCanRight());
        	menuItemPrev.setEnabled(isSelected && getMainController().itemsListCanLeft());
        	menuItemLabel.setEnabled(isSelected);
        	menuItemOpenBrowser.setEnabled(isSelected);
        	menuItemcopyUrlToBrowser.setEnabled(isSelected);
        	menuItemSourceCode.setEnabled(isSelected);
        	menuItemSpamLabeled.setEnabled(isSelected);
        	try {
				menuItemViewItemAll.setEnabled(!getMainController().getItemMenuViewAllStatus());
	        	menuItemViewItemNew.setEnabled(getMainController().getItemMenuViewAllStatus());
			} catch (BaseException e1) {
				getMainController().showError(e1);
			}
        	feedsPopup.show(e.getComponent(), e.getX(), e.getY());
        }
    }
	public String getBundleMessage(String message) {
		return getMainController().getBundleMessage(message);
	}
}
